<template>
	<el-card>
		<el-table border :data="skuList" v-loading="loading">
			<el-table-column
				label="序号"
				type="index"
				align="center"
				width="80"
			></el-table-column>
			<el-table-column label="名称" prop="skuName"></el-table-column>
			<el-table-column label="描述" prop="skuDesc"></el-table-column>
			<el-table-column label="默认图片">
				<template v-slot="{ row }">
					<el-image :src="row.skuDefaultImg" style="width: 100px"></el-image>
				</template>
			</el-table-column>
			<el-table-column label="重量(KG)" prop="weight"></el-table-column>
			<el-table-column label="价格(元)" prop="price"></el-table-column>
			<el-table-column label="操作" width="260px" align="center">
				<template v-slot="{ row, $index }">
					<el-tooltip
						class="box-item"
						effect="dark"
						content="下架"
						placement="top"
					>
						<el-button
							size="small"
							v-show="row.isSale === 0"
							type="success"
							:icon="Bottom"
							@click="toCancelSale(row)"
						></el-button>
					</el-tooltip>
					<el-tooltip
						class="box-item"
						effect="dark"
						content="上架"
						placement="top"
					>
						<el-button
							size="small"
							v-show="row.isSale !== 0"
							type="info"
							:icon="Top"
							@click="toOnSale(row)"
						></el-button>
					</el-tooltip>

					<el-button size="small" type="primary" :icon="Edit"></el-button>

					<el-tooltip
						class="box-item"
						effect="dark"
						content="查看"
						placement="top"
					>
						<el-button
							size="small"
							type="info"
							:icon="InfoFilled"
							@click="toCheckSkuInfo(row)"
						></el-button>
					</el-tooltip>
					<el-popconfirm
						width="220"
						confirm-button-text="确定"
						cancel-button-text="取消"
						:icon="InfoFilled"
						icon-color="#626AEF"
						title="确定删除当前SKU数据吗？"
						@confirm="toDeleteSkuInfo(row, $index)"
						@cancel="cancelEvent"
					>
						<template #reference>
							<el-button size="small" type="danger" :icon="Delete"></el-button>
						</template>
					</el-popconfirm>
				</template>
			</el-table-column>
		</el-table>

		<el-pagination
			v-model:current-page="currentPage"
			v-model:page-size="pageSize"
			:page-sizes="[3, 6, 9, 12]"
			:small="small"
			:disabled="disabled"
			:background="background"
			layout=" prev, pager, next, jumper, ->,sizes,total"
			:total="total"
			@size-change="handleSizeChange"
			@current-change="handleCurrentChange"
		/>

		<!-- 抽屉 -->
		<el-drawer
			class="skuInfo"
			size="50%"
			v-model="drawerVisible"
			title="查看SKUINFO"
			direction="rtl"
		>
			<el-row :gutter="20">
				<el-col :span="6"><h4>名称</h4></el-col>
				<el-col :span="18">{{ skuInfo.skuName }}</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="6"><h4>描述</h4></el-col>
				<el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="6"><h4>价格</h4></el-col>
				<el-col :span="18">{{ skuInfo.price }}.00</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="6"><h4>平台属性</h4></el-col>
				<el-col :span="18">
					<sl-tag
						class="ml-2"
						style="margin-right: 4px"
						v-for="attrValue in skuInfo.skuAttrValueList"
						type="success"
						:key="attrValue.id"
						>{{ attrValue.valueName }}</sl-tag
					>
				</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="6"><h4>销售属性</h4></el-col>
				<el-col :span="18">
					<el-tag
						class="ml-2"
						style="margin-right: 4px"
						v-for="saleAttrValue in skuInfo.skuSaleAttrValueList"
						type="success"
						:key="saleAttrValue.id"
						>{{ saleAttrValue.saleAttrValueName }}</el-tag
					>
				</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="6"><h4>商品图片</h4></el-col>
				<el-col :span="18">
					<el-carousel :interval="2000">
						<el-carousel-item
							v-for="item in skuInfo.skuImageList"
							:key="item.id"
						>
							<el-image :src="item.imgUrl"></el-image>
						</el-carousel-item>
					</el-carousel>
				</el-col>
			</el-row>
		</el-drawer>
	</el-card>
</template>

<script lang="ts">
export default {
	name: 'SSku',
}
</script>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { InfoFilled, Edit, Delete, Top, Bottom } from '@element-plus/icons-vue'
import {
	reqDeleteSkuInfo,
	reqGetAllSkuListData,
	reqGetSkuInfoData,
	reqSkuCancelSale,
	reqSkuOnSale,
} from '@/api/product/sku'
import { ElMessage } from 'element-plus'

const currentPage = ref(1)
const pageSize = ref(10)
const skuList = ref([])
const total = ref(0)
const small = ref(false)
const loading = ref(true)
const background = ref(false)
const disabled = ref(false)

const drawerVisible = ref(false)
const skuInfo = ref({})

// 获取skulist数据
const getAllSkuListData = async () => {
	loading.value = true
	const result = await reqGetAllSkuListData(currentPage.value, pageSize.value)
	loading.value = false
	skuList.value = result.records
	total.value = result.total
}
onMounted(() => {
	getAllSkuListData()
})
// 单击分页
const handleCurrentChange = (val: number) => {
	currentPage.value = val
	getAllSkuListData()
}
// 修改每页数量
const handleSizeChange = (val: number) => {
	pageSize.value = val
	currentPage.value = 1
	getAllSkuListData()
}
// 下架商品
const toCancelSale = async (row) => {
	await reqSkuCancelSale(row.id)
	ElMessage.success('商品下架成功')
	row.isSale = 1 - row.isSale
}
// 上架商品
const toOnSale = async (row) => {
	await reqSkuOnSale(row.id)
	ElMessage.success('商品上架成功')
	row.isSale = 1 - row.isSale
}
// 删除skuInfo商品
const toDeleteSkuInfo = async (row, index) => {
	await reqDeleteSkuInfo(row.id)
	ElMessage.success('删除SKU成功')
	skuList.value.splice(index, 1)
}
// 取消删除
const cancelEvent = () => {
	ElMessage.info('取消删除成功')
}
// 查看skuInfo数据
const toCheckSkuInfo = async (row) => {
	const result = await reqGetSkuInfoData(row.id)
	skuInfo.value = result
	drawerVisible.value = true
}
</script>

<style lang="scss" scoped>
.el-carousel__item h3 {
	color: #475669;
	opacity: 0.75;
	line-height: 300px;
	margin: 0;
	text-align: center;
}

.el-carousel__item:nth-child(2n) {
	background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
	background-color: #d3dce6;
}

.el-drawer__body h4 {
	margin: 0;
	padding: 0;
	height: 40px;
	text-align: right;
}
.el-drawer__body {
	.el-image {
		.el-image__inner {
			// width: 80%;
			height: 90%;
		}
	}
}
</style>
